<script setup>
import { ref } from 'vue'
import { Plus } from "@element-plus/icons-vue";
import useTable from '@/utils/useTable'
import {http} from '@/api'
import {ElMessage} from "element-plus";

const categoryList = ref([])
http.Role.list().then(res => {
  categoryList.value = res.data
})
/**
 * 表格
 * @type {Ref<UnwrapRef<*[]>>}
 */
const {
  searchParams,
  pageNo,
  pageSize,
  total,
  changePage,
  changeSize,
  dataSource,
  search,
  reset,
  openAdd,
  openView,
  openEdit,
  openDelete
} = useTable({
  requestFn: http.Account,
  routerPre: 'Account',
  params: {
    "realName": "",
  }
})

const changeStatus = (row) => {
  http.Account.updateStatus({
    id: row.id,
    status: row.status
  }).then(res => {
    ElMessage.success('操作成功')
  }).catch(() => {
    if (row.status === 1) {
      row.status = 0
    } else {
      row.status = 1
    }
  })
}
</script>


<template>
  <PageMain title="管理员管理">
    <el-form label-suffix="：" inline>
      <el-form-item label="用户名称">
        <el-input clearable v-model="searchParams.realName"></el-input>
      </el-form-item>
      <el-form-item label="角色">
        <el-select v-model="searchParams.roleId">
          <el-option v-for="item in categoryList" :label="item.role" :value="item.id" :key="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search">查询</el-button>
        <el-button @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
    <div class="add-box">
      <el-button type="primary" :icon="Plus" @click="openAdd">添加</el-button>
    </div>
    <div class="table-box">
      <el-table :data="dataSource">
        <el-table-column label="用户名称" prop="realName"/>
        <el-table-column label="用户账号" prop="username" />
        <el-table-column label="状态">
          <template #default="scope">
            <div style="display: flex; align-items: center">
              <el-switch @change="() => changeStatus(scope.row)" v-model="scope.row.status" :active-value="0" :inactive-value="1"/>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="角色" property="roleName"/>
        <el-table-column label="操作" fixed="right">
          <template #default="scope">
            <el-button type="text" @click="openEdit(scope.row)">编辑</el-button>
            <el-button type="text" @click="openDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </PageMain>
</template>

<style scoped lang="scss">

</style>
